package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.TrafficUvCt;
import com.atguigu.gmall.publisher.service.TrafficStatsService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Felix
 * @date 2023/2/13
 * 流量域统计Controller
 */
@RestController
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    @RequestMapping("/ch")
    public String getChUvCt(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> uvCtList = trafficStatsService.getChUvCt(date, limit);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": [");
        for (int i = 0; i < uvCtList.size(); i++) {
            TrafficUvCt uvCt = uvCtList.get(i);
            jsonB.append("{\"name\": \"" + uvCt.getCh() + "\",\"value\": " + uvCt.getUvCt() + "}");
            if(i < uvCtList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("]}");
        return jsonB.toString();
    }

}
